function MainAssistant() {
	/* this is the creator function for your scene assistant object. It will be passed all the 
	   additional parameters (after the scene name) that were passed to pushScene. The reference
	   to the scene controller (this.controller) has not be established yet, so any initialization
	   that needs the scene controller should be done in the setup function below. */
	   
}

MainAssistant.prototype.setup = function() {
	/* this function is for setup tasks that have to happen when the scene is first created */
		
	/* use Mojo.View.render to render view templates and add them to the scene, if needed */
	this.controller.setupWidget(Mojo.Menu.appMenu, AddSubMenuAttr, AddSubMenuModel);
	
	/* setup widgets here */

	this.viewMenuModel = {
		visible: true,
		items: [{
			items: [
					{ label: AppSettings.getLastUpdateTime() + "     (" + (MCModels.couponList.length) + ")", width: 260 },
					{ command: 'do-refresh', icon: "refresh" }
			]
		}]
	};
	this.controller.setupWidget(Mojo.Menu.viewMenu,
	  this.viewMenuAttributes = {
		  spacerHeight: 50,
		  menuClass: 'no-fade'
	  },
	  this.viewMenuModel
	); 

	this.bottomMenuModel = {
		visible: true,
		items: [
			{
				items: [ 
					{ iconPath: "smallicon.png", command: 'do-buy'}
				]
			}
		]
	};
	this.controller.setupWidget( Mojo.Menu.commandMenu, {}, this.bottomMenuModel );
	

	this.controller.setupWidget("ajaxSpinner", this.spinnerAttrs = {
        spinnerSize: Mojo.Widget.spinnerLarge
    }, this.spinnerModel = {
        spinning: true
    });
	
	
	this.toggleFullorBuyOnly = true;
    
    this.scrim = Mojo.View.createScrim(this.controller.document, {
        scrimClass: 'palm-scrim'
    });
    this.controller.get("ajaxLoading").appendChild(this.scrim).appendChild($("ajaxSpinner"));
    this.scrim.hide();
	
	
	
	this.controller.setupWidget("couponListWgt",
		this.couponListAttr = {
			itemTemplate: "main/couponItem-Template",
			swipeToDelete: false,
			renderLimit: 40,
			reorderable: false
		},
		this.couponListModel = {
			items: []
		}
	)

	this.checkboxCouponAttribs = {
		modelProperty: 'checked',
		disabledProperty: 'disabled'
	}
	
	this.controller.setupWidget('checkboxCoupon', this.checkboxCouponAttribs);
	
	/* add event handlers to listen to events from widgets */
	this.loadCoupon();

}

MainAssistant.prototype.activate = function(event) {
	/* put in event handlers here that should only be in effect when this scene is active. For
	   example, key handlers that are observing the document */
    //this.controller.listen(this.controller.window, Mojo.Event.flick, this.onFlick.bind(this));	

	this.handleListSubWidgetChanges = this.handleListWidgetChanges.bindAsEventListener(this);
 
    // In order to hear the checkbox event changes, this must be listened to
    this.controller.listen(this.controller.get("couponListWgt"), Mojo.Event.listChange, this.handleListSubWidgetChanges);
    // This is the event that the checkbox sends when it changes state, but we listen on the list
    this.controller.listen(this.controller.get("couponListWgt"), Mojo.Event.propertyChange, this.handleListSubWidgetChanges);
	   
}

MainAssistant.prototype.handleListWidgetChanges = function(event){
    // Handle the property change of the checkbox
    // event.model is the item that the checkbox is using as its model

}

MainAssistant.prototype.deactivate = function(event) {
	/* remove any event handlers you added in activate and do any other cleanup that should happen before
	   this scene is popped or another scene is pushed on top */
    //this.controller.stopListening(this.controller.window, Mojo.Event.flick, this.onFlick);	   
    this.controller.stopListening(this.controller.get("couponListWgt"), Mojo.Event.listChange, this.handleListSubWidgetChanges);
    // This is the event that the checkbox sends when it changes state, but we listen on the list
    this.controller.stopListening(this.controller.get("couponListWgt"), Mojo.Event.propertyChange, this.handleListSubWidgetChanges);
	
}

MainAssistant.prototype.cleanup = function(event) {
	/* this function should do any cleanup needed before the scene is destroyed as 
	   a result of being popped off the scene stack */

	//save couponList
	AppSettings.saveCoupon();
}


MainAssistant.prototype.handleCommand = function(event) {
  if (event.type === Mojo.Event.command) {
      switch (event.command) {
		case 'do-refresh':
			this.refreshCoupon();
			break;
		case 'do-buy':
			this.toggleFullorBuyOnly = !this.toggleFullorBuyOnly;

			if (this.toggleFullorBuyOnly == true) {
				this.couponListModel.items = MCModels.getCouponList();
				this.controller.modelChanged(this.couponListModel);
				
			} else {
				this.couponListModel.items = MCModels.getBuyList();
				this.controller.modelChanged(this.couponListModel);
			}
			break;
      }
  }
}



MainAssistant.prototype.refreshCoupon = function() {


	this.scrim.show();
	
	var request = new Ajax.Request(AppSettings.CouponRequestURI , {
		encoding: 'UTF-8',
		bodyencoding: 'UTF-8',
		method: "GET",
		evalJSON: "false",
		onSuccess: this.refreshCouponSuccess.bind(this),
		onFailure: this.refreshCouponFailure.bind(this)
	});

	
}

MainAssistant.prototype.refreshCouponSuccess = function(transport) {

    this.scrim.hide();

	try {
		var htmlstring = transport.responseText;
		var couponstring = htmlstring.match(AppSettings.RegExp);
		
		MCModels.couponList = [];
		for (var i=0;i<couponstring.length;i++) {
			
			var startPos = couponstring[i].indexOf('"') + 1;
			var stopPos = couponstring[i].lastIndexOf('"');
			if (startPos >=0 && stopPos >=0) {
				var imageUrl = couponstring[i].substring(startPos,stopPos);
				//imageUrl = imageUrl.replace("../images/mclub/","");
				imageUrl = AppSettings.CouponDirectoryURI + imageUrl;
				var coupon = {"imageUrl":imageUrl }; //, "localImageUrl":imageUrl};
				MCModels.couponList.push(coupon);
			}
		}
		
		//update the display
		this.couponListModel.items = MCModels.couponList;
		this.controller.modelChanged(this.couponListModel);
		//update the last updated time
		AppSettings.setLastUpdateTime(new Date());
		
		this.updateViewMenu();
		
		//download image to local cache
		this.downloadImages();
	}
	catch(e){
		Mojo.Log.error(e);
	}
}

MainAssistant.prototype.refreshCouponFailure = function(transport) {

    this.scrim.hide();
	Mojo.Log.error ("refreshCouponFailure");
}


MainAssistant.prototype.updateViewMenu = function() {

	this.viewMenuModel.items = [{
			items: [
					{ label: AppSettings.getLastUpdateTime() + "     (" + (MCModels.couponList.length) + ")", width: 260 },
					{ command: 'do-refresh', icon: "refresh" }
			]
		}];
	this.controller.modelChanged(this.viewMenuModel);	
}

MainAssistant.prototype.loadCoupon = function() {
	//Load coupon from local cache
	//if local cache is empty, refresh from web
	AppSettings.loadCoupon();
	if (MCModels.couponList) {
		//display coupon list
		this.couponListModel.items = MCModels.couponList;
		this.controller.modelChanged(this.couponListModel);
		this.updateViewMenu();		
		
	} else {
		this.refreshCoupon();
	}
}


MainAssistant.prototype.downloadImages = function() {

	try
	{
		for (var i=0; i < MCModels.couponList.length; i++) {
			this.controller.serviceRequest('palm://com.palm.downloadmanager/', 
			{
				method: 'download',
				parameters: {
						target: MCModels.couponList[i].imageUrl,
						targetDir: '/media/internal/.mcdonaldscoupon/',
						targetFilename: i + ".jpg",
						keepFilenameOnRedirect: false,
						subscribe: false
						 },
				onSuccess : this.downloadImagesSuccess(i),			
				onFailure : function (error){ Mojo.Log.error(error)}
			})
		}
	}	
	catch(error){
		Mojo.Log.error (error);
	}

}


MainAssistant.prototype.downloadImagesSuccess = function(couponIndex) {
	MCModels.couponList[couponIndex].localImageUrl="/media/internal/.mcdonaldscoupon/" + couponIndex + ".jpg";
	MCModels.couponList[couponIndex].imageUrl=MCModels.couponList[couponIndex].localImageUrl;
}
